package main

import (
	"cas_data_acquisition/preprocessor"
	"errors"

	"github.com/jinzhu/configor"
)

// Config 采集卡配置
type Config struct {
	Name              string `yaml:"name" required:"true"`
	SamplesPerSecond  int    `yaml:"samplesPerSecond" required:"true"`
	PreProcessor      string `yaml:"preProcessor" default:""`
	Mock              bool   `yaml:"mock" default:"false"`
	Unit              string `yaml:"unit" default:""`
	CentralServerIP   string `yaml:"centralServerIP" required:"true"`
	CentralServerPort int    `yaml:"centralServerPort" required:"true"`
	HostPort          string `yaml:"hostPort" required:"true"`
	DB                string `yaml:"db" required:"true"`
	User              string `yaml:"user" required:"true"`
	Password          string `yaml:"password" required:"true"`
	WebServerPort     int    `yaml:"webServerPort" required:"true"`
}

var conf = Config{}

func loadConfig() error {
	if err := configor.New(&configor.Config{ErrorOnUnmatchedKeys: true}).Load(&conf, "./config.yml"); err != nil {
		return err
	}
	// 自定义检查
	if conf.SamplesPerSecond%10 != 0 {
		return errors.New("samplesPerSecond 字段必须是10的倍数")
	}
	if conf.PreProcessor != "" && !preprocessor.ExistPreProcessor(conf.PreProcessor) {
		return errors.New("数据预处理器" + conf.PreProcessor + "不存在，请检查名称是否正确")
	}
	return nil
}
